package com.test.mooan.labama.view_boom.module_boom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.test.mooan.labama.R;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.utils_boom.Stringutil_boom;

import java.util.ArrayList;

public class ProcessView_boom extends LinearLayout {

    public ProcessView_boom(Context context) {
        super(context);
        initView(context);
    }

    public ProcessView_boom(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ProcessView_boom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){

        View view = View.inflate(context, R.layout.view_process_boom,this);

        LinearLayout iconGroupLayout = view.findViewById(R.id.view_process_layout_icon_group);
        LinearLayout textGroupLayout = view.findViewById(R.id.view_process_layout_text_group);

        ArrayList<String> allSteps = ProcessInfo_boom.getInstance().gAllSteps();
        ArrayList<String>  allStepsTitle = ProcessInfo_boom.getInstance().gAllStepTitles();
        ArrayList<String> steps = ProcessInfo_boom.getInstance().gSteps();

        if (steps==null||steps.isEmpty()) return;

        boolean nowProcessFlag = true;
        for (int i=0;i<allSteps.size();i++){

            View nameView = View.inflate(context,R.layout.view_process_text_boom,null);
            TextView nameTv = nameView.findViewById(R.id.view_process_text_tv_text);
            nameView.setLayoutParams(new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1));
            textGroupLayout.addView(nameView);

            nameTv.setText(Stringutil_boom.processTitleFormat(allSteps.get(i)));

            View iconView = View.inflate(context,R.layout.view_process_unit_boom,null);
            View iconBgLayout = iconView.findViewById(R.id.view_process_unit_layout_bg);

            View lineParentView = View.inflate(context,R.layout.view_line_boom,null);
            View lineChildView = lineParentView.findViewById(R.id.view_line);
            lineParentView.setLayoutParams(new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1));

            //已完成
            if (!steps.contains(allSteps.get(i))){
                iconBgLayout.setBackgroundResource(R.drawable.ic_process_unit_finish);
                lineChildView.setBackgroundColor(ContextCompat.getColor(context,R.color.green_boom));
                nameTv.setTextColor(ContextCompat.getColor(context,R.color.green_boom));
            }else
            //进行中
            if (steps.contains(allSteps.get(i))&&nowProcessFlag){
                iconBgLayout.setBackgroundResource(R.drawable.ic_process_unit_now);
                nameTv.setTextColor(ContextCompat.getColor(context,R.color.blue_boom));
                nowProcessFlag=false;
            }
            //未完成
            iconGroupLayout.addView(iconView);
            if (i<allSteps.size()-1) iconGroupLayout.addView(lineParentView);

            if ((i==allSteps.size()-1)){

                iconView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){
                    @Override
                    public boolean onPreDraw() {
                        iconView.getViewTreeObserver().removeOnPreDrawListener(this);

                        int groupWidth = iconGroupLayout.getWidth();
                        int iconWidth = iconView.getWidth();

                        if (iconWidth<(groupWidth/allSteps.size())){
                            int padding = ((groupWidth/allSteps.size())-iconWidth)/2;
                            iconGroupLayout.setPadding(padding,0,padding,0);
                        }
                        return false;
                    }
                });

            }
        }

    }

}
